package com.example.skycodes.dao;

import com.example.skycodes.bean.AdminBean;
import com.example.skycodes.util.DBConnection;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import static javax.security.auth.callback.ConfirmationCallback.OK;

public class AdminDao {
    DBConnection DBConn = new DBConnection();
    public boolean CheckLogin(String s1,String s2) throws SQLException {
        // DBConn对象 代表了MySQL数据库sky连接
        Connection conn = DBConn.getConn();
        Statement stmt = conn.createStatement();
        ResultSet rs = null;//结果集
        boolean OK = true;// true 登录成功 false登录失败
        // todo 密码加密 账户密码过滤
        String sql = "select * from admin where Admin Name = " + s1 + " and AdminPwd = " + s2;
        rs = stmt.executeQuery(sql);//执行sql语句
        if(!rs.next()){
            OK = false;
        }
        return OK;
    }
}
    public List<AdminBean> getList(int page) {
        Connection conn = DBConn.getConn();
        String sql = "select * from admin limit "+(page-1)*10+",10";
        List<AdminBean> list = new ArrayList<AdminBean>();
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                AdminBean adminBean = new AdminBean();
                adminBean.setAdminId(rs.getInt("AdminID"));
                adminBean.setAdminPwd(rs.getString("AdminPwd"));
                adminBean.setAdminType(rs.getInt("AdminType"));
                adminBean.setLastLoginTime(rs.getString("LastLoginTime"));
                adminBean.setAdminName(rs.getString("AdminName"));
                list.add(adminBean);
            }
        } catch (SQLException e) {
            throw new RuntimeException();
        }
        return list;
    }

    /**
     * 添加新闻
     *
     * @param
     * @return
     */
    public int add(AdminBean admin) {
        Connection conn = DBConn.getConn();
        String sql = "insert into admin value(null,?,?,?,?)";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, admin.getAdminName());
            pmst.setString(2, admin.getAdminPwd());
            pmst.setInt(3, admin.getAdminType());
            pmst.setString(4, (new Date()).toString());
            return pmst.executeUpdate(); // 执行添加 返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 更新功能
     *
     * @param
     * @return
     */
    public int update(AdminBean admin) {
        Connection conn = DBConn.getConn();
        String sql = "update admin set AdminName=?, AdminPwd=?, AdminType=?, LastLoginTime=? where AdminID=?";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, admin.getAdminName());
            pmst.setString(2, admin.getAdminPwd());
            pmst.setInt(3, admin.getAdminType());
            pmst.setString(4, (new Date()).toString());
            pmst.setInt(5, admin.getAdminId());
            System.out.println("执行sql：" + pmst.toString());
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public int count() {
        try {
            Connection conn = DBConn.getConn();
            try {
                String sql = "select count(*) from admin";
                Statement statement = conn.createStatement();
                ResultSet rs = statement.executeQuery(sql);
                if (rs.next()) {
                    return rs.getInt(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        } finally {

        }
    }
                }
